﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

using prjKQXS_DLL;

namespace prjKQXS_UI.View
{
    public partial class frmHoaHongCFG : DevExpress.XtraEditors.XtraForm
    {
        public frmHoaHongCFG()
        {
            InitializeComponent();
            
        }
        
        SqlConnection conn = new SqlConnection(BienTc.connString);
        private void frmHoaHongCFG_Load(object sender, EventArgs e)
        {                      
            loadData(isPublic);
            loadLoaiHD();
        }
        private bool isPublic = true;
        public void loadData(bool isPublic)
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {                    
                    conn.Open();
                }
                SqlDataAdapter da = new SqlDataAdapter("Select * from HOAHONGCFG Where isPublic=@isPublic", conn);
                da.SelectCommand.Parameters.AddWithValue("@isPublic", isPublic);
                DataTable dt = new DataTable();
                da.Fill(dt);
                gcHoaHong.DataSource = dt;
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không kết nối được máy chủ !", 0);
            }
            finally
            {
                conn.Close();
            }
        }

        public void loadLoaiHD()
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {                    
                    conn.Open();
                }
                SqlDataAdapter da = new SqlDataAdapter("Select Ma_DT,Ten_DT,ID, Ma_DT +' - '+Ten_DT as LoaiHD from DMLOAIHOPDONG Where isPublic=1", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                rpLoaiHD.DataSource = dt;
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không kết nối được máy chủ !", 0);
            }
            finally
            {
                conn.Close();
            }
        }

        private void btnThemmoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (chkKhoiPhuc.Checked == true)
                {
                    clsCommon.deleteRestoreRow("HOAHONGCFG", "ID", gvHoaHong.GetFocusedDataRow()["ID"].ToString(), true, false);
                    loadData(isPublic);
                }
                else
                {
                    if (AddData() > 0)
                    {
                        loadData(isPublic);
                    }
                }
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không kết nối được máy chủ !", 0);
            }
        }

        public int AddData()
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {                    
                    conn.Open();
                }
                SqlCommand cmd = new SqlCommand("Insert into HOAHONGCFG(Muc_Tien,Hoa_Hong) values(0,0)", conn);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không kết nối được máy chủ !", 0);
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (chkKhoiPhuc.Checked == false)
                {
                    clsCommon.deleteRestoreRow("HOAHONGCFG", "ID", gvHoaHong.GetFocusedDataRow()["ID"].ToString(), false, false);
                }
                else
                {
                    clsCommon.deleteRestoreRow("HOAHONGCFG", "ID", gvHoaHong.GetFocusedDataRow()["ID"].ToString(), false, true);
                }
                loadData(isPublic);
            }
            catch (Exception)
            {
                clsCommon.MsgBox("Không kết nối được máy chủ !", 0);
            } 
        }

        private void chkKhoiPhuc_CheckedChanged(object sender, EventArgs e)
        {
            if (chkKhoiPhuc.Checked == true)
            {
                btnThemmoi.Text = "&Khôi phục";
                btnThemmoi.ImageIndex = 2;
                isPublic = false;
            }
            else
            {
                btnThemmoi.Text = "&Thêm mới";
                btnThemmoi.ImageIndex = 7;
                isPublic = true;
            }
            loadData(isPublic);
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gvHoaHong_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            clsCommon.updateRowValue("HOAHONGCFG", e.Column.FieldName, "Where ID=" + gvHoaHong.GetFocusedDataRow()["ID"].ToString(), e.Value);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSearch.Tag.Equals("LOC"))
                {
                    btnSearch.Tag = "BL";
                    btnSearch.Text = "&Bỏ lọc";
                    gvHoaHong.OptionsView.ShowAutoFilterRow = true;
                }
                else
                {
                    gvHoaHong.OptionsView.ShowAutoFilterRow = false;
                    btnSearch.Tag = "LOC";
                    btnSearch.Text = "&Lọc";
                }
            }
            catch
            {
            }
        }
    }
}